<?php
// 读取JSON文件
$jsonFile = 'config.json';
$jsonData = file_get_contents($jsonFile);
$config = json_decode($jsonData, true);

// 处理新增配置请求
if (isset($_GET['action']) && $_GET['action'] == 'add') {
    $newAppName = $_GET['new_app_name'];
    if (!empty($newAppName) && !isset($config['configs'][$newAppName])) {
        $config['configs'][$newAppName] = $config['default']; // 使用默认配置初始化
        file_put_contents($jsonFile, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        header("Location: index.php?app=" . urlencode($newAppName));
        exit;
    }
}

// 处理删除配置请求
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    $appToDelete = $_GET['app'];
    if (isset($config['configs'][$appToDelete])) {
        unset($config['configs'][$appToDelete]);
        file_put_contents($jsonFile, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        header("Location: index.php");
        exit;
    }
}

// 处理重命名配置请求
if (isset($_POST['rename_action'])) {
    $oldName = $_POST['old_name'];
    $newName = $_POST['new_name'];
    if (isset($config['configs'][$oldName]) && !empty($newName) && !isset($config['configs'][$newName])) {
        $config['configs'][$newName] = $config['configs'][$oldName];
        unset($config['configs'][$oldName]);
        file_put_contents($jsonFile, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        header("Location: index.php?app=" . urlencode($newName));
        exit;
    }
}

$appNames = array_keys($config['configs']);
$currentApp = isset($_GET['app']) ? $_GET['app'] : (count($appNames) > 0 ? $appNames[0] : '');
$currentConfig = $currentApp ? $config['configs'][$currentApp] : $config['default'];
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON配置编辑器</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { padding: 20px; background-color: #f8f9fa; }
        .form-container { background: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }
        .app-selector { margin-bottom: 20px; }
        .form-group { margin-bottom: 15px; }
        .array-item { display: flex; margin-bottom: 5px; }
        .array-item input { flex-grow: 1; margin-right: 10px; }
        .config-management { margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center mb-4">页面注册机配置编辑器</h1>
             <!-- 配置管理区域 -->
        <div class="config-management">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">配置管理</h5>
                </div>
                <div class="card-body">
                    <!-- 新增配置表单 -->
                    <form method="GET" class="row g-3 align-items-center mb-3">
                        <div class="col-auto">
                            <label class="form-label">新增配置：</label>
                        </div>
                        <div class="col-auto">
                            <input type="text" class="form-control" name="new_app_name" placeholder="输入新配置名称" required>
                        </div>
                        <div class="col-auto">
                            <input type="hidden" name="action" value="add">
                            <button type="submit" class="btn btn-success">添加</button>
                        </div>
                    </form>
                    
                    <!-- 重命名配置表单 -->
                    <?php if ($currentApp): ?>
                    <form method="POST" class="row g-3 align-items-center mb-3">
                        <div class="col-auto">
                            <label class="form-label">重命名当前配置：</label>
                        </div>
                        <div class="col-auto">
                            <input type="text" class="form-control" name="new_name" placeholder="新名称" required>
                            <input type="hidden" name="old_name" value="<?php echo htmlspecialchars($currentApp); ?>">
                            <input type="hidden" name="rename_action" value="1">
                        </div>
                        <div class="col-auto">
                            <button type="submit" class="btn btn-warning">重命名</button>
                        </div>
                    </form>
                    
                    <!-- 删除配置按钮 -->
                    <div class="row g-3 align-items-center">
                        <div class="col-auto">
                            <label class="form-label">删除当前配置：</label>
                        </div>
                        <div class="col-auto">
                            <a href="?action=delete&app=<?php echo urlencode($currentApp); ?>" 
                               class="btn btn-danger" 
                               onclick="return confirm('确定要删除此配置吗？')">删除</a>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        

        <!-- 配置选择区域 -->
        <div class="app-selector">
            <form method="GET" class="row g-3 align-items-center">
                <div class="col-auto">
                    <label class="form-label">选择应用配置：</label>
                </div>
                <div class="col-auto">
                    <select name="app" class="form-select" onchange="this.form.submit()">
                        <option value="">默认配置</option>
                        <?php foreach ($appNames as $appName) { ?>
                            <option value="<?php echo htmlspecialchars($appName); ?>" <?php echo $currentApp === $appName ? 'selected' : ''; ?>>
                                <?php echo htmlspecialchars($appName); ?>
                            </option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col-auto">
                    <a href="?app=" class="btn btn-secondary">编辑默认配置</a>
                </div>
            </form>
        </div>
        
        <!-- 配置编辑区域 -->
        <div class="form-container">
            <form action="save.php" method="POST">
                <input type="hidden" name="app" value="<?php echo htmlspecialchars($currentApp); ?>">
                
                <div class="form-group form-check">
    <input type="hidden" name="开关" value="false"> <!-- 添加隐藏字段，默认值为false -->
    <input type="checkbox" class="form-check-input" id="switch" name="开关" value="true" <?php echo $currentConfig['开关'] ? 'checked' : ''; ?>>
    <label class="form-check-label" for="switch">开关</label>
</div>
                
                <div class="form-group">
                    <label for="class">类名</label>
                    <input type="text" class="form-control" id="class" name="类" value="<?php echo htmlspecialchars($currentConfig['类'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label for="background">背景</label>
                    <input type="text" class="form-control" id="background" name="背景" value="<?php echo htmlspecialchars($currentConfig['背景'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label for="color">颜色</label>
                    <input type="color" class="form-control" id="color" name="颜色" value="<?php echo htmlspecialchars($currentConfig['颜色'] ?? '#6200EE'); ?>">
                </div>
                
                <div class="form-group">
                    <label for="video">视频URL</label>
                    <input type="url" class="form-control" id="video" name="视频" value="<?php echo htmlspecialchars($currentConfig['视频'] ?? ''); ?>">
                </div>
                
                <div class="form-group form-check">
    <input type="hidden" name="启用按钮" value="false"> <!-- 添加隐藏字段，默认值为false -->
    <input type="checkbox" class="form-check-input" id="enableButton" name="启用按钮" value="true" <?php echo $currentConfig['启用按钮'] ?? false ? 'checked' : ''; ?>>
    <label class="form-check-label" for="enableButton">启用按钮</label>
</div>
                
                <div class="form-group">
                    <label for="buttonText">按钮文本</label>
                    <input type="text" class="form-control" id="buttonText" name="按钮文本" value="<?php echo htmlspecialchars($currentConfig['按钮文本'] ?? ''); ?>">
                </div>
                
                <div class="form-group">
                    <label for="buttonAction">按钮动作</label>
                    <select class="form-select" id="buttonAction" name="按钮动作">
                        <option value="qq" <?php echo ($currentConfig['按钮动作'] ?? '') === 'qq' ? 'selected' : ''; ?>>QQ</option>
                        <option value="qun" <?php echo ($currentConfig['按钮动作'] ?? '') === 'qun' ? 'selected' : ''; ?>>QQ群</option>
                        <option value="url" <?php echo ($currentConfig['按钮动作'] ?? '') === 'url' ? 'selected' : ''; ?>>URL</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="param">参数</label>
                    <input type="text" class="form-control" id="param" name="参数" value="<?php echo htmlspecialchars($currentConfig['参数'] ?? ''); ?>">
                </div>
                

<div class="form-group form-check">
    <input type="hidden" name="激活功能" value="false"> <!-- 添加隐藏字段，默认值为false -->
    <input type="checkbox" class="form-check-input" id="enableFeature" name="激活功能" value="true" <?php echo $currentConfig['激活功能'] ?? false ? 'checked' : ''; ?>>
    <label class="form-check-label" for="enableFeature">激活功能</label>
</div>

                
                <div class="form-group">
                    <label for="welcome">欢迎语</label>
                    <textarea class="form-control" id="welcome" name="欢迎语" rows="3"><?php echo htmlspecialchars($currentConfig['欢迎语'] ?? ''); ?></textarea>
                </div>
                
                <div class="form-group">
                    <label>黑名单</label>
                    <div id="blacklistContainer">
                        <?php if (!empty($currentConfig['黑名单'])) { ?>
                            <?php foreach ($currentConfig['黑名单'] as $item) { ?>
                                <div class="array-item">
                                    <input type="text" class="form-control" name="黑名单[]" value="<?php echo htmlspecialchars($item); ?>">
                                    <button type="button" class="btn btn-danger btn-sm remove-item">删除</button>
                                </div>
                            <?php } ?>
                        <?php } ?>
                    </div>
                    <button type="button" class="btn btn-secondary btn-sm mt-2" id="addBlacklist">添加黑名单项</button>
                </div>
                
                <button type="submit" class="btn btn-primary">保存配置</button>
            </form>
        </div>
    </div>
   
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 添加黑名单项
        document.getElementById('addBlacklist').addEventListener('click', function() {
            const container = document.getElementById('blacklistContainer');
            const div = document.createElement('div');
            div.className = 'array-item';
            div.innerHTML = `
                <input type="text" class="form-control" name="黑名单[]" value="">
                <button type="button" class="btn btn-danger btn-sm remove-item">删除</button>
            `;
            container.appendChild(div);
        });
        
        // 删除黑名单项
        document.addEventListener('click', function(e) {
            if (e.target.classList.contains('remove-item')) {
                e.target.parentElement.remove();
            }
        });
    </script>
</body>
</html>